package xyz.bali16.module.sign.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.application.entity.Role;
import xyz.bali16.module.sign.entity.SignEvent;
import xyz.bali16.module.sign.entity.SignPoint;
import xyz.bali16.module.sign.model.SignEventAddParam;
import xyz.bali16.module.sign.model.SignEventPageParam;
import xyz.bali16.module.sign.model.SignEventUpdateParam;
import xyz.bali16.module.sign.model.SignPointPageParam;
import xyz.bali16.module.sign.service.SignEventService;

import javax.validation.Valid;
import java.util.List;

import static xyz.bali16.application.core.model.Result.*;
import static xyz.bali16.application.core.model.Result.failure;
import static xyz.bali16.module.sign.utils.ResultUtils.errorLog;

/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@RestController
@RequestMapping(BaseController.API+"sign/event")
@Api(tags = "签到活动活动接口")
@AllArgsConstructor

public class SignEventController extends BaseController{
    public static final Logger logger = LoggerFactory.getLogger(SignEventController.class);
    @Autowired
    SignEventService signEventService;
    @GetMapping
    @ApiOperation("分页条件查询活动列表")
    //@PreAuthorize("@auth.permission('plugin:signevent:query')")
    public Result<IPage<SignEvent>> pageQuery(@Valid SignEventPageParam param) {
        IPage<SignEvent> signEventIPage = signEventService.getPage(param);
        return success(ApiEnum.GET_SUCCESS, signEventIPage);
    }
    //@GetMapping
    //////@PreAuthorize("@auth.permission('user:update')")
    //@ApiOperation(value = "查询签到活动列表")
    //public Result<List<SignEvent>> getList() {
    //    LambdaQueryWrapper<SignEvent> wrapper = Wrappers.<SignEvent>lambdaQuery()
    //            .orderByDesc(SignEvent::getStartTime);
    //    return success(ApiEnum.GET_SUCCESS, signEventService.list(wrapper));
    //}

    @PostMapping
    @ApiOperation("添加签到活动")
    //@PreAuthorize("@auth.permission('plugin:signevent:add')")
    public Result<String> add(@RequestBody @Valid SignEventAddParam param) {
        try {
            signEventService.addSignEvent(param);
            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "新增签到活动 '{}' 信息失败 {}", param.getName(), e.getMessage());
            return failure(ApiEnum.ADD_ERROR, "新增签到活动失败", e);
        }
    }

    @PutMapping
    ////@PreAuthorize("@auth.permission('user:update')")
    @ApiOperation("修改签到活动")
    //@PreAuthorize("@auth.permission('plugin:signevent:update')")
    public Result<String> update(@RequestBody @Valid SignEventUpdateParam param) {
        try {
            signEventService.updateSignEvent(param);
            return response(ApiEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "更新签到活动 '{}' 信息失败 {}", param.getId(), e.getMessage());
            return failure(ApiEnum.UPDATE_ERROR, "更新签到活动失败", e);
        }
    }

    @DeleteMapping("/{signId}")
    ////@PreAuthorize("@auth.permission('user:delete')")
    @ApiOperation("删除签到活动")
    //@PreAuthorize("@auth.permission('plugin:signevent:delete')")
    @ApiImplicitParam(name = "signId", value = "签到活动id", paramType = "path", required = true)
    public Result<String> deleteNav(@PathVariable("signId") String signId) {
        try {
            signEventService.delete(signId);
            return response(ApiEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            errorLog(logger, e, "删除签到活动 '{}' 失败. {}", signId, e.getMessage());
            return failure(ApiEnum.DELETE_ERROR, e);
        }
    }

}
